package com.example.lab06.controller;

import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
//1. 注册为spring一个类
//2. 返回的都是json类型
@RequestMapping("redis")
public class RedisController {
    private final RedisTemplate<String, String> redisTemplate;

    public RedisController(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @GetMapping("getString")
    private String getString() {
        redisTemplate.delete("username");
        redisTemplate.opsForValue().set("username", "tom");//操作字符串类型方法 set添加一个值
        return redisTemplate.opsForValue().get("username");
    }

    @GetMapping("getList")
    private List<String> getList() {
        redisTemplate.delete("username");
        redisTemplate.opsForList().rightPush("username", "Tom");
        redisTemplate.opsForList().rightPush("username", "Jerry");
        redisTemplate.opsForList().rightPushAll("username", "TOM", "JERRY");
        return redisTemplate.opsForList().range("username", 0, -1);
    }

    @GetMapping("getHash")
    private Map<Object, Object> getHash() {
        redisTemplate.delete("user");
//        redisTemplate.opsForHash().put("user","username","Tom");
//        redisTemplate.opsForHash().put("user","password","123");
//        redisTemplate.opsForHash().put("user","gender","Male");
//        redisTemplate.opsForHash().put("user","age","24");
        Map<String, String> map = new LinkedHashMap<>();
        map.put("username", "jerry");
        map.put("password", "456");
        map.put("gender", ",Male");
        map.put("age", "24");
        redisTemplate.opsForHash().putAll("user", map);
        return redisTemplate.opsForHash().entries("user");
    }

    @GetMapping("getSet")
    private Set<String> getSet() {
        redisTemplate.delete("course");
        redisTemplate.opsForSet().add("course", "JAVA SE", "JAVA EE", "JAVA EE FRAMEWORK", "DATABASE", "Linux");
        return redisTemplate.opsForSet().members("course");
    }

    @GetMapping("getZSet")
    private Set<String> getZSet() {
        redisTemplate.delete("languages");
        redisTemplate.opsForZSet().add("languages", "HTML", 1);
        redisTemplate.opsForZSet().add("languages", "CSS", 2);
        redisTemplate.opsForZSet().add("languages", "JavaScript", 3);
        redisTemplate.opsForZSet().add("languages", "Java", 4);
        redisTemplate.opsForZSet().add("languages", "mysql", 5);
        return redisTemplate.opsForZSet().range("languages", 0, -1);
    }

    @GetMapping("getGeo")
    private boolean geiGeo() {
        String key = "China_geo";
        {
            try {
                BufferedReader bufferedReader = new BufferedReader(new FileReader("lab-06/data/geo.csv"));
                String line, city, longitude, latitude;
                Map<String, Point> map = new LinkedHashMap<>();
                while ((line = bufferedReader.readLine()) != null) {
                    String[] info = line.split(",");
                    city = info[0];
                    longitude = info[1];
                    latitude = info[2];

                    Point point = new Point(Double.parseDouble(longitude), Double.parseDouble(latitude));
                    map.put(city, point);

                    System.out.println(city + ":" + longitude + ":" + latitude);
                }
                BoundGeoOperations<String, String> boundGeoOperations = redisTemplate.boundGeoOps(key);
                boundGeoOperations.add(map);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }

}
